#include "Acceptor.h"

void Acceptor::HandleRead()
{
    int newfd = _socket.Accept();
    if (newfd < 0)
        return;
    if (_accept_callback)
        _accept_callback(newfd);
}
int Acceptor::CreateServer(uint64_t port)
{
    bool ret = _socket.CreateServer(port);
    if (!ret)
        abort();
    return _socket.Fd();
}

Acceptor::Acceptor(EventLoop *loop, int port)
    : _socket(CreateServer(port)),
      _loop(loop),
      _channel(loop, _socket.Fd())
{
    _channel.SetReadCallback(std::bind(&Acceptor::HandleRead, this));
}
// 1. 设置新链接的回调函数
void Acceptor::SetAcceptCallback(const AcceptCallback &cb)
{
    _accept_callback = cb;
}
void Acceptor::Listen()
{
    _channel.EnableRead();
}